package com.spark.actions.sys;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.spark.actions.BaseAction;
import com.spark.core.Message;
import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.dao.SessionFactory;

@SuppressWarnings("unchecked")
public class DefaultAction extends BaseAction {
	
	protected boolean validateInset(Map map, Message message){
		return false;
	}
	
	protected boolean validateUpdate(Map map, Message message){
		return false;
	}
	
	protected void writeConflictingMessage(Map map, Message message){
		
	}
	
	protected void initModel(SparkContext context, Map map){
		setUserId(map, context);
		setCompanyId(map, context);
		setAccountSetId(map,context);
	}

	protected void doQuery(SparkContext context, String statement) throws Exception {
		Map params = context.getParameterMap();
		XMap request = context.getParameter();
		Message message = new Message(true);
		Integer pageSize = request.getInt(BaseAction.QUERY_PAGE_SIZE);
		SqlSession sqlSession = SessionFactory.getSession();
		try {
			if (pageSize!=-1) {
				BaseAction.parsePageSizeAndPageNum(context, params);
				Integer total = (Integer) sqlSession.selectOne(statement.concat(BaseAction.STATMENT_COUNT), params);
				message.put(BaseAction.TOTAL, total);
			}
			List list = sqlSession.selectList(statement, params);
			message.setResult(list);
		} finally {
			SessionFactory.closeSession(sqlSession);
		}
		writeMessage(context, message);
	}
	
	protected void doInsert(SparkContext context,SqlSession sqlSession, String statement, Map map, Message message){
		sqlSession.insert(statement, map);
	}
	
	protected void doDelete(SparkContext context,SqlSession sqlSession,String statement, List list, Message message){
		Iterator it = list.iterator();
		while(it.hasNext()){
			Map data = (Map)it.next();			
			sqlSession.delete(statement, data);
		}
		message.setResult(list);
	}
	
	protected void doUpdate(SparkContext context,SqlSession sqlSession,String statement, Map map, Message message){
		sqlSession.update(statement, map);
	}
	
	protected void doExecute(SparkContext context, String statement, String action) throws Exception{
		Map requestData = getRequestData(context);
		List list = (List)requestData.get(PARAMETER);
		Message message = new Message(true);
		if(list!=null && list.size() > 0){
			Map map = (Map)list.get(0);
			SqlSession sqlSession = SessionFactory.getSession(false);
			try {
				initModel(context,map);
				if(ACTION_INSERT.equals(action) && validateInset(map,message)){
					doInsert(context,sqlSession,statement, map,message);
				}else if(ACTION_UPDATE.equals(action) && validateUpdate(map,message)) {
					doUpdate(context,sqlSession,statement,map,message);
				}else if(ACTION_DELETE.equals(action)) {
					doDelete(context,sqlSession,statement,list,message);
				}
				sqlSession.commit();
			}catch(PersistenceException e){
				if(e.getCause() instanceof MySQLIntegrityConstraintViolationException){
					writeConflictingMessage(map, message);
				}else {
					throw e;
				} 
			} finally {
				SessionFactory.closeSession(sqlSession);
			}
		}
		writeMessage(context, message);
	}
	
}	
